package test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;

@SuppressWarnings("deprecation")
public class POSTUpload {

	public static void main(String[] args) {
		String url = "http://saucenao.com/";
		File file = new File("C:\\Users\\Aidturith\\Desktop\\41764898.png");
		
		//CloseableHttpClient client = HttpClients.createDefault();
		
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);

		MultipartEntity entity = new MultipartEntity();
		entity.addPart("file", new FileBody(file));
		post.setEntity(entity);
		
		CookieStore cookieStore = new BasicCookieStore();
		BasicClientCookie cookie1 = new BasicClientCookie("__utma", "4212273.1196486134.1394369822.1394369822.1394405089.2;");
		BasicClientCookie cookie2 = new BasicClientCookie("__utmz", "4212273.1394369822.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);");
		BasicClientCookie cookie3 = new BasicClientCookie("__utmb", "4212273.2.10.1394405089;");
		BasicClientCookie cookie4 = new BasicClientCookie("__utmc", "4212273");

		cookieStore.addCookie(cookie1);
		cookieStore.addCookie(cookie2);
		cookieStore.addCookie(cookie3);
		cookieStore.addCookie(cookie4);
		((AbstractHttpClient) client).setCookieStore(cookieStore);

		byte[] buf = new byte[1024];
		String tmp = "";
		try {
		    HttpResponse response = client.execute(post);
			System.out.println(response.getStatusLine());
			
			int cpt = 0;
			while(response.getEntity().getContent().read() != -1)
				cpt++;
			System.out.println(cpt);
			
			/*while(response.getEntity().getContent().read(buf) != -1)
				System.out.println(new String(buf));*/
		   // EntityUtils.consume(entity);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
